<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class myBucket implements Countable{
    const SBUCKET = 'bucket';
    const SNAME_PID = 'product_id'; 
    const SNAME_NMB = 'number';
    
    private $objects;
    private $user;
    private $id;
    
    private function __construct() {
        
        $this->id = 1;
        $this->objects = array();
    }
    
    private function load(){
        $i = 0;
        $this->value = 0;
        while($i < 10000){
            if(isset($_SESSION[self::SBUCKET . '_' . self::SNAME_PID . '_' . $i])){
                $pid = $_SESSION[self::SBUCKET . '_' . self::SNAME_PID . '_' . $i];
                $nmb = $_SESSION[self::SBUCKET . '_' . self::SNAME_NMB . '_' . $i];
                $obj = $this->addObject($i, $pid, $nmb);
                $this->value += $obj['pidobj']->getPrice() * $nmb;
            }
            $i++;
        }
    }
    
    public function setUser($user) {
        $this->user = $user;
        $this->load();
    }
    
    public function addObject($i, $pid, $nmb){
        $pid = ProductTable::getInstance()->findOneBy('id', $pid);
        $this->id = $i;
        $obj = array("id" => $i, "pid" => $pid->getId(),  "nmb" => $nmb);
        $obj['pidobj'] = $pid;
        $this->objects[] = $obj;
        return $obj;
    }
    
    public function addNewObject($pid, $nmb){
        for($i = 0; $i < 10000;$i++){
            if(!isset($_SESSION[self::SBUCKET . '_' . self::SNAME_PID . '_' . $i])){
                $this->addObject($i, $pid, $nmb);
                break;
            }
        }
    }
    
    public function addObjectCount($id, $count){
        foreach($this->objects as $key => $object){
            if($object['id'] == $id){
                $this->objects[$key]['nmb'] = $count;
                return true;
            }
        }
        return false;
    }
    
    public function removeObject($id){
        foreach($this->objects as $key => $object){
            if($object['id'] == $id){
                unset($_SESSION[self::SBUCKET . '_' . self::SNAME_PID . '_' . $id]);
                unset($_SESSION[self::SBUCKET . '_' . self::SNAME_NMB . '_' . $id]);
                unset($this->objects[$key]);
                return true;
            }
        }
        return false;
    }
    
    public function getObjects(){
        return $this->objects;
    }
    
    public static function getInstance(){
        static $inst;
        if(!isset($inst)){
            $inst = new self();
        }
        return $inst;
    }
     
    public function save(){
        foreach($this->objects as $key => $object){
            $_SESSION[self::SBUCKET . '_' . self::SNAME_PID . '_' . $key] = $object['pid'];
            $_SESSION[self::SBUCKET . '_' . self::SNAME_NMB . '_' . $key] = $object['nmb'];
        }
    }
    
    public function clean(){
        foreach($this->objects as $key => $object){
            $this->user->setAttribute(self::SBUCKET . '_' . self::SNAME_PID . '_' . $key, NULL);
            $this->user->setAttribute(self::SBUCKET . '_' . self::SNAME_NMB . '_' . $key, NULL);
        }
        unset($this->value);
        unset($this->objects);
        unset($this->id);
        unset($this->user);
    }
    
    public function getValue(){
        $value = 0;
        foreach($this->objects as $object){
            $value += ($object['pidobj']->getPrice() * $object['nmb']);
        }
        return $value;
    }
    
    public function getUser(){
        return $this->user;
    }
    
    public function count(){
        return count($this->objects);
    }
}